package com.alt.ui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.alt.base.ALTTabActivity;
import com.alt.handler.CMCmdHandler;
import com.alt.manager.ALTAppMgr;
import com.alt.manager.ALTPTCMgr;
import com.alt.manager.ALTPTCMgr.SCREEN_UI;
import com.alt.models.TransactionData;
import com.alt.R;

public class ALTTransactionHistory extends ALTTabActivity {

	private TableLayout tblTransaction;
	private Button btnEarned , btnUsed;
	private TextView tvTotalUserFee1,tvHow;
	private TransactionData[] value;
	private SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);// HH:mm

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_transaction_history);
		onSetScreenMode(SCREEN_UI.TRANSACTION_HISTORY);

		tblTransaction = (TableLayout) findViewById(R.id.tblTransaction);
		btnEarned = (Button) findViewById(R.id.btnEarned);
		btnUsed = (Button) findViewById(R.id.btnUsed);
		
		tvTotalUserFee1 = (TextView) findViewById(R.id.tvTotalUserFee1);		
		tvHow = (TextView) findViewById(R.id.tvHow);
		
		tvTotalUserFee1.setText("Bạn đang có: " +ALTAppMgr.getInstance().getUserLogInfo().TotalUserFee+" đồng");
		
		btnEarned.setOnClickListener(new OnClickListener() {						
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnEarned.setBackgroundColor(getResources().getColor(R.color.tab1));
				btnUsed.setBackgroundColor(getResources().getColor(R.color.tab2));
				
				tvHow.setText("Kiếm được");
				
				tblTransaction.removeAllViews();
				
				  for (int i = 0; i < value.length; i++) {			
					  if(value[i].TransGroupId.equalsIgnoreCase(ALTPTCMgr.TRANSACTION_TYPE.CLICK.toString()))
					      addrow(df.format(Date.parse(value[i].TransDate)) + "\n" + value[i].AdsId, "Quảng cáo" ,value[i].UserFee + "đ");
					  else if (value[i].TransGroupId.equalsIgnoreCase(ALTPTCMgr.TRANSACTION_TYPE.INVITE.toString()))
						  addrow(df.format(Date.parse(value[i].TransDate)) + "\n" + value[i].InvitedUserId, "Mời bạn" ,value[i].UserFee + "đ");
				  }	
			}
		});
		
		btnUsed.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnEarned.setBackgroundColor(getResources().getColor(R.color.tab2));
				btnUsed.setBackgroundColor(getResources().getColor(R.color.tab1));
				
				tvHow.setText("Tốn hết");
				
				tblTransaction.removeAllViews();							
				
				  for (int i = 0; i < value.length; i++) {			
					  if(value[i].TransGroupId.equalsIgnoreCase(ALTPTCMgr.TRANSACTION_TYPE.EXCHANGE.toString()))
					      addrow(df.format(Date.parse(value[i].TransDate)), "Đổi Card" ,value[i].UserFee + "đ");
				  }	
			}
		});		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		  Bundle b = new Bundle();
		  b.putString("userid", ALTAppMgr.getInstance().getUserLogInfo().UserId);
		  
		  ALTAppMgr.getInstance().startServiceCmd(CMCmdHandler.COMMAND_GET_TRANSACTION_lIST_BY_USERID,new CMCmdHandler(), b,true);
	}
	
	@Override
	 public void onWindowFocusChanged(boolean hasFocus) {
	  // TODO Auto-generated method stub
	  super.onWindowFocusChanged(hasFocus);
	  //Here you can get the size!	  
	 }
	
	public void addrow(String content,String type,String fee){
		int wi = tblTransaction.getWidth();			
		/* Create a new row to be added. */
		TableRow tr = new TableRow(this);
		tr.setPadding(0, 1, 0, 0);
		tr.setWeightSum(10);
		
		tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 60));
		/* Create a Button to be the row-content. */		
		TextView b = new TextView(this);
		b.setText(content);
		b.setBackgroundResource(R.color.tblrow);
		b.setTextColor(Color.BLACK);
		b.setPadding(7, 0, 0, 0);
		b.setLines(2);
		b.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		TableRow.LayoutParams l1 = new TableRow.LayoutParams(Double.valueOf(wi*0.5).intValue(), TableRow.LayoutParams.FILL_PARENT);
		l1.setMargins(0, 0, 1, 0);	
		b.setLayoutParams(l1);

		
		TextView c = new TextView(this);
		c.setText(type);		
		c.setBackgroundResource(R.color.tblrow);
		c.setTextColor(Color.BLACK);
		c.setGravity(Gravity.CENTER);
		TableRow.LayoutParams l2 = new TableRow.LayoutParams(Double.valueOf(wi*0.25).intValue(), TableRow.LayoutParams.FILL_PARENT);
		l2.setMargins(0, 0, 1, 0);		
		c.setLayoutParams(l2);
		
		TextView d = new TextView(this);
		d.setText(fee);
		d.setBackgroundResource(R.color.tblrow);
		d.setTextColor(Color.BLACK);
		d.setGravity(Gravity.CENTER);
		d.setLayoutParams(new TableRow.LayoutParams(Double.valueOf(wi*0.25).intValue(), TableRow.LayoutParams.FILL_PARENT));
		
		/* Add Button to row. */
		tr.addView(b);
		tr.addView(c);
		tr.addView(d);
		/* Add row to TableLayout. */
		//tr.setBackgroundResource(R.drawable.sf_gradient_03);
		tblTransaction.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
	}
	
	@Override
	public void processCommandResult(int commandCode, Object resultData) {
		// TODO Auto-generated method stub
		super.processCommandResult(commandCode, resultData);
		
		Bundle bResult = (Bundle) resultData;

		switch(commandCode){				     
			case CMCmdHandler.COMMAND_GET_TRANSACTION_lIST_BY_USERID:{
				value = ( TransactionData[])bResult.getSerializable(ALTPTCMgr.REST_OBJECT_RETURN);				
				
				  for (int i = 0; i < value.length; i++) {			
					  if(value[i].TransGroupId.equalsIgnoreCase(ALTPTCMgr.TRANSACTION_TYPE.CLICK.toString()))
					      addrow(df.format(Date.parse(value[i].TransDate)) + "\n" + value[i].AdsId, "Quảng cáo" ,value[i].UserFee + "đ");
					  else if (value[i].TransGroupId.equalsIgnoreCase(ALTPTCMgr.TRANSACTION_TYPE.INVITE.toString()))
						  addrow(df.format(Date.parse(value[i].TransDate)) + "\n" + value[i].InvitedUserId, "Mời bạn" ,value[i].UserFee + "đ");
				  }						
			}
				break;
		 
		 default:
			 break;		
		}			
	}
	

	   @Override
	public void onScreenChanged(SCREEN_UI curScreen) {
		// TODO Auto-generated method stub
		super.onScreenChanged(curScreen);
	}


}
